package com.ruoyix.ai.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ai/chat")
public class OpenAIChatController {

    @Autowired
    private  OpenAiChatModel chatModel;

    @Autowired
    public OpenAIChatController(OpenAiChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/generate")
    @SaIgnore
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        ChatResponse response = chatModel.call(
            new Prompt(
                message,
                OpenAiChatOptions.builder()
                    .withModel("gpt-4o")
                    .build()
            ));

        Map<String,Object> map = new HashMap<>();
        map.put("generation", response);
        return map;
    }


}
